<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/27 0027
 * Time: 15:49
 */

namespace app\api\model\Index;


use app\api\model\Course\CourseModel;
use app\api\model\Goods\GoodsModel;
use app\api\model\User\UserModel;
use app\api\model\Video\VideoModel;
use app\Utils\ImageUtil;
use think\Model;
use think\facade\Db;

class VideoUser extends Model
{

    /**
     * 表名常量
     */
    const Table = 'video_user';

    /**
     * 数据表名
     * @var string
     */
    protected $name = 'video_user';


    /**
     * @return \think\model\relation\HasOne
     * 用户
     */
    public function user()
    {
        return $this->hasOne(UserModel::class, 'id', 'user_id')->field('id,head_img,nickname');
    }

    /**
     * @return \think\model\relation\HasOne
     * 线上视频
     */
    public function video()
    {
        return $this->hasOne(VideoModel::class, 'id', 'video_id');
    }


    /**
     * 视频关联商品
     * $goods_id 商品id
     */
    public static function goodsinfo(int $goods_id)
    {
        if ($goods_id == 0 || $goods_id == null) return null;
        $info = GoodsModel::getGoodsWhere(['id' => $goods_id], 'id,good_name,img,price');
        $info['img'] = ImageUtil::i()->replaceUrl($info['img']);
        return $info;
    }

    public static function courseinfo(int $courseid)
    {
        if ($courseid == 0 || $courseid == null) return null;

        $info = CourseModel::getCourseWhere(['id' => $courseid], 'id,title,img,price');
        $info['img'] = ImageUtil::i()->replaceUrl($info['img']);
        return $info;
    }
}
